/*
 * Copyright (c) Ulrich Hilger, http://dev.uhilger.de, all rights reserved.
 *
 * Published under the terms and conditions of the Apache License 2.0,
 * see http://www.apache.org/licenses/LICENSE-2.0
 */

package de.uhilger.servicehub.view;

import de.uhilger.servicehub.ServiceHub;
import de.uhilger.servicehub.ctrl.action.AbstractServiceHubAction;
import de.uhilger.servicehub.model.UserStore;
import java.util.ResourceBundle;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

/**
 * Main panel of applicatin ServiceHub
 *
 * @author Ulrich Hilger, <a href="http://dev.uhilger.de" target="_blank">http://dev.uhilger.de</a>
 * @author Published under the terms and conditions of
 * the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License 2.0</a>
 */
public class MainPanel extends JTabbedPane {

  private static final String RN_MAIN_PANEL_SERVER_TAB_LABEL = "mainPanelServerTabLabel";
  private static final String RN_MAIN_PANEL_APP_TAB_LABEL = "mainPanelAppTabLabel";
  private static final String RN_MAIN_PANEL_ROLES_TAB_LABEL = "mainPanelRolesTabLabel";
  private static final String RN_MAIN_PANEL_USER_TAB_LABEL = "mainPanelUserTabLabel";

  /** index of server tab */
  public static final int TAB_SERVER = 0;
  /** index of applications tab */
  public static final int TAB_APPS = 1;
  /** index of roles tab */
  public static final int TAB_ROLES = 2;
  /** index of users tab */
  public static final int TAB_USERS = 3;

  private ServerPanel serverPanel;
  private AppPanel appPanel;
  private RolePanel rolesPanel;
  private UserPanel userPanel;
  private ServiceHub owner;

  /**
   * Create a new object of class MainPanel
   * @param owner
   */
  public MainPanel(ServiceHub owner) {
    super();
    this.owner = owner;
    buildUi();
  }

  /**
   * Set the user store to use on the user and role panels
   * @param store  the user store
   */
  public void setUserStore(UserStore store) {
    rolesPanel.setUserStore(store);
    userPanel.setUserStore(store);
  }

  public AppPanel getAppPanel() {
    return appPanel;
  }

  public ServerPanel getServerPanel() {
    return serverPanel;
  }

  /**
   * Create the user interface
   */
  private void buildUi() {
    ResourceBundle res = owner.getBundle();
    serverPanel = new ServerPanel(owner, /*owner.getServer(),*/
      (AbstractServiceHubAction) owner.getAction(ServiceHub.ACTION_DIRECTORY_SELECT));
    this.add(res.getString(RN_MAIN_PANEL_SERVER_TAB_LABEL), serverPanel);
    appPanel = new AppPanel((AbstractServiceHubAction) owner.getAction(ServiceHub.ACTION_FILE_SELECT), res);
    this.add(res.getString(RN_MAIN_PANEL_APP_TAB_LABEL), appPanel);
    rolesPanel = new RolePanel(owner.getBundle());
    this.add(res.getString(RN_MAIN_PANEL_ROLES_TAB_LABEL), rolesPanel);
    userPanel = new UserPanel(owner.getBundle());
    this.add(res.getString(RN_MAIN_PANEL_USER_TAB_LABEL), userPanel);
    this.addChangeListener(userPanel);
    this.setBorder(new EmptyBorder(6, 6, 6, 6));
  }

}
